/*
 * File Name：NetworkUtils.java
 * Copyright：Copyright 2008-2014 CiWong.Inc. All Rights Reserved.
 * Description： NetworkUtils.java
 * Modify By：res-jxliu
 * Modify Date：2014-5-28
 * Modify Type：Add
 */
package com.ciwong.libs.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 
 * 网络处理类
 * 
 * @author res-jxliu
 * @version ciwong v.1.0 2014-5-28
 * @since ciwong v.1.0
 */
public class NetworkUtils
{
    private static Context mContext;

    /**
     * 
     * 使用NetworkUtils类之前，必须设置context， 否则报错 最好使用application的context
     * 
     * @param context
     *            上下文
     */
    public static void setContext(Context context)
    {
        mContext = context;
    }

    /**
     * 
     * 获取上下文
     * 
     * @return mContext
     */
    public static Context getContext()
    {
        return mContext;
    }

    /**
     * 获取网络联接管理
     * 
     * @return ConnectivityManager
     */
    public static ConnectivityManager getConnManager()
    {
        return (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
    }

    /**
     * 判断网络联接是否可用
     * 
     * @return 网络联接是否可用
     */
    public static boolean isOnline()
    {
        NetworkInfo info = getConnManager().getActiveNetworkInfo();
        if (info != null && info.isConnected())
        {
            return true;
        }
        return false;
    }

    /**
     * 判断wifi是否可用
     * 
     * @return wifi是否可用
     */
    public static boolean isWifiOnline()
    {
        NetworkInfo info = getConnManager().getNetworkInfo(
                ConnectivityManager.TYPE_WIFI);
        if (info != null && info.isConnected())
        {
            return true;
        }
        return false;
    }

    /**
     * 开启/关闭 移动数据
     * 
     * 
     * @param enabled
     *            true:打开;false:关闭
     */
    public static void start3G(boolean enabled)
    {
        ConnectivityManager connectivityManager = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        // ConnectivityManager类
        Class<?> connectivityManagerClass = null;
        // ConnectivityManager类中的字段
        Field connectivityManagerField = null;

        // IConnectivityManager接口
        Class<?> iConnectivityManagerClass = null;
        // IConnectivityManager接口的对象
        Object iConnectivityManagerObject = null;
        // IConnectivityManager接口的对象的方法
        Method setMobileDataEnabledMethod = null;

        try
        {
            // 取得ConnectivityManager类
            connectivityManagerClass = Class.forName(connectivityManager
                    .getClass().getName());
            // 取得ConnectivityManager类中的字段mService
            connectivityManagerField = connectivityManagerClass
                    .getDeclaredField("mService");
            // 取消访问私有字段的合法性检查
            // 该方法来自java.lang.reflect.AccessibleObject
            connectivityManagerField.setAccessible(true);

            // 实例化mService
            // 该get()方法来自java.lang.reflect.Field
            // 一定要注意该get()方法的参数:
            // 它是mService所属类的对象
            iConnectivityManagerObject = connectivityManagerField
                    .get(connectivityManager);
            // 得到mService所属接口的Class
            iConnectivityManagerClass = Class
                    .forName(iConnectivityManagerObject.getClass().getName());
            // 取得IConnectivityManager接口中的setMobileDataEnabled(boolean)方法
            // 该方法来自java.lang.Class.getDeclaredMethod
            setMobileDataEnabledMethod = iConnectivityManagerClass
                    .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
            // 取消访问私有方法的合法性检查
            // 该方法来自java.lang.reflect.AccessibleObject
            setMobileDataEnabledMethod.setAccessible(true);
            // 调用setMobileDataEnabled方法
            setMobileDataEnabledMethod.invoke(iConnectivityManagerObject,
                    enabled);
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (NoSuchFieldException e)
        {
            e.printStackTrace();
        }
        catch (SecurityException e)
        {
            e.printStackTrace();
        }
        catch (NoSuchMethodException e)
        {
            e.printStackTrace();
        }
        catch (IllegalArgumentException e)
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
        catch (InvocationTargetException e)
        {
            e.printStackTrace();
        }

    }

    /**
     * 
     * 获取移动数据的状态
     * 
     * 
     * @return boolean
     */
    public static boolean getMobileDataStatus()
    {
        String methodName = "getMobileDataEnabled";
        ConnectivityManager mConnectivityManager = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        Class cmClass = mConnectivityManager.getClass();
        Boolean isOpen = null;

        try
        {
//            Method method = cmClass.getMethod(methodName, null);
//
//            isOpen = (Boolean) method.invoke(mConnectivityManager, null);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return isOpen;
    }

    /**
     * 
     * true 3G是连接的；false 3G是关闭
     * 
     * 
     * @return boolean
     */
    public static boolean is3GAvailable()
    {
        ConnectivityManager connectivity = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null)
        {
            return false;
        }
        else
        {
            NetworkInfo info = connectivity
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (info == null)
            {
                return false;
            }
            else
            {
                if (info.isConnectedOrConnecting())
                {
                    return true;
                }
            }
        }

        return false;
    }
}
